#!/bin/bash

# RuoYi-TDesign Docker 管理脚本

case "$1" in
    start)
        echo "启动所有服务..."
        docker-compose up -d
        ;;
    stop)
        echo "停止所有服务..."
        docker-compose down
        ;;
    restart)
        echo "重启所有服务..."
        docker-compose down
        docker-compose up -d
        ;;
    status)
        echo "查看服务状态..."
        docker-compose ps
        ;;
    logs)
        echo "查看服务日志..."
        if [ -z "$2" ]; then
            docker-compose logs -f
        else
            docker-compose logs -f $2
        fi
        ;;
    build)
        echo "构建并启动服务..."
        # 构建后端
        cd ruoyi-admin
        mvn clean package -DskipTests
        cd ..
        
        # 构建前端
        cd ruoyi-ui
        npm install
        npm run build
        cd ..
        
        # 启动服务
        docker-compose up -d
        ;;
    init-db)
        echo "初始化数据库..."
        ./init-db.sh
        ;;
    clean)
        echo "清理所有数据..."
        docker-compose down -v
        rm -rf ./logs
        rm -rf ./script/docker/mysql/data
        rm -rf ./script/docker/redis/data
        rm -rf ./script/docker/minio/data
        ;;
    help)
        echo "可用命令:"
        echo "  start     - 启动所有服务"
        echo "  stop      - 停止所有服务"
        echo "  restart   - 重启所有服务"
        echo "  status    - 查看服务状态"
        echo "  logs      - 查看服务日志"
        echo "  build     - 构建并启动服务"
        echo "  init-db   - 初始化数据库"
        echo "  clean     - 清理所有数据"
        echo "  help      - 显示帮助信息"
        ;;
    *)
        echo "用法: $0 {start|stop|restart|status|logs|build|init-db|clean|help}"
        echo "使用 '$0 help' 查看详细帮助信息"
        exit 1
        ;;
esac